#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
This file is part of playlistbs.

@author: Romain RICHARD
@contact: romain.richard.IT.engineer@gmail.com
@version: 2.0
@copyright: GPLv3

@change: 2012-09-17: Used pylint to clean up the code
@change: 2011-11-20: Commented out some options
@change: 2011-08-15: Added the --sort-by-date and --dont-download-subtitles
                     options
@change: 2011-08-06: Added the --auto-add-shows option
@change: 2011-08-02: Use argparse for the parameters
@change: 2011-08-01: Creation of the file
"""

from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser

from __init__ import __version__
from playlist_betaseries.playlistbs import PlaylistBS


PARSER = ArgumentParser(
    formatter_class=ArgumentDefaultsHelpFormatter,
    description="""Display episodes the user hasn't seen yet (according to
                   www.betaseries.com) and create a playlist of these episodes.
                   """,
    prog="playlistbs"
)

PARSER.add_argument("-v", "--version",
                    action="version",
                    version="%(prog)s {version}".format(version=__version__)
                    )

GROUP = PARSER.add_argument_group("Setup")
GROUP.add_argument("-c", "--config-folder",
                   type=str,
                   metavar="<path>",
                   dest="config_folder",
                   default="~/.playlistbs",
                   help="""The folder where the config file is and other files
                   are located / will be created"""
                   )

GROUP = PARSER.add_argument_group("Credentials",
                                  "User's credentials for www.betaseries.com")
GROUP.add_argument("-l", "--login",
                   type=str,
                   metavar="<login>",
                   dest="login",
                   help="Your login"
                   )
GROUP.add_argument("-p", "--password",
                   type=str,
                   metavar="<password>",
                   dest="password",
                   help="Your password"
                   )
GROUP.add_argument("-P", "--password-md5",
                   type=str,
                   metavar="<hash>",
                   dest="password_md5",
                   help="""The hash of your password
                   (override the -p / --password option)"""
                   )

GROUP = PARSER.add_argument_group("Miscellaneous")
#GROUP.add_argument("-d", "--daemon",
#                   type=str,
#                   metavar="<action>",
#                   dest="daemon",
#                   help="""Daemonize playlistbs
#                   (available options: start|stop|restart|update)"""
#                   )
GROUP.add_argument("-u", "--update",
                   action="store_true",
                   dest="update",
                   help="Force the update"
                   )
GROUP.add_argument("-U", "--update-frequency",
                   type=int,
                   metavar="<seconds>",
                   dest="update_frequency",
                   default=3600,
                   help="The frequency in seconds of the auto-update"
                   )
GROUP.add_argument("-M", "--dont-mark-episodes-downloaded",
                   action="store_false",
                   dest="mark_episodes_downloaded",
                   help="Don't mark the episodes as downloaded"
                   )

GROUP = PARSER.add_argument_group("Display")
GROUP.add_argument("-L", "--limit",
                   type=int,
                   metavar="<limit>",
                   dest="limit",
                   default=-1,
                   help="""Limit the number of episodes to display and match
                   for each show (will also be used for the playlist)"""
                   )
GROUP.add_argument("-s", "--sort-by-date",
                   action="store_true",
                   dest="sort_by_date",
                   help="""Sort the episodes by date, from the oldest to the
                   newest (will also be used for the playlist) (by default, the
                   episodes are sorted using your sorted shows list)"""
                   )

GROUP = PARSER.add_argument_group("Subtitles")
GROUP.add_argument("-S", "--subtitles-to-download",
                   type=int,
                   metavar="<max>",
                   dest="subtitles_to_download",
                   default=-1,
                   help="""The number of subtitles for an episode, in each
                   language, to download (0 = don't download subtitles)"""
                   )

GROUP = PARSER.add_argument_group("Playlist")
GROUP.add_argument("-F", "--folder",
                   type=str,
                   metavar="<path>",
                   dest="folder",
                   default=".",
                   help="The folder where the the episodes are"
                   )
GROUP.add_argument("-D", "--dont-create-playlist",
                   action="store_false",
                   dest="create_playlist",
                   help="""Don't create the playlist (faster, only display the
                   episodes)"""
                   )
GROUP.add_argument("--pilots",
                   action="store_true",
                   dest="pilots",
                   help="""Add at the end of the playlist the pilots of new
                   shows that you didn't add yet to your betaseries shows list
                   """
                   )
GROUP.add_argument("--auto-add-shows",
                   action="store_true",
                   dest="auto_add_shows",
                   help="""Automatically add shows found on your computer to
                   your betaseries account"""
                   )

ARGS = PARSER.parse_args()

PLAYLISTBS = PlaylistBS(ARGS.config_folder)
PLAYLISTBS.set_config(vars(ARGS))

if not PLAYLISTBS.get_login() or not PLAYLISTBS.get_password():
    print("You must enter your login and password")
    PARSER.print_help()
    exit()

if ARGS.update or not PLAYLISTBS.is_uptodate():
    PLAYLISTBS.login()
    PLAYLISTBS.update_shows_list()
    PLAYLISTBS.update_episodes_list()
    if PLAYLISTBS.get_mark_episodes_downloaded():
        PLAYLISTBS.mark_episodes_downloaded()
    if PLAYLISTBS.get_download_subtitles():
        PLAYLISTBS.download_subtitles()
    if PLAYLISTBS.get_create_playlist():
        PLAYLISTBS.create_playlist()

PLAYLISTBS.display_episodes()
